﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using PPI.Client.Logic.XML;
using PPI.Entities.Kml;
using PPI.Entities.Settings;
using PPI.WebConsumer;
using VMind.Global;

namespace PPI.Client.Logic
{
    public class LSettings
    {
        public static ESettingsBase GetSetting(ESettingsBase settingBase)
        {
            DataTable dtSettings = WebConsumer.WSettings.GetSettingByKey(settingBase.SettingKey).Tables[0];
            return ParseSetting(settingBase, dtSettings);
        }

        public static void UpdateSetting(ESettingsBase setting)
        {
            WebConsumer.WSettings.UpdateSettingByKey(setting.SettingKey,setting.SettingText,setting.SettingValue);
        }

        private static ESettingsBase ParseSetting(ESettingsBase settingBase, DataTable dtSettings)
        {
            if(dtSettings.Rows.Count > 0)
            {
                settingBase.SettingId = ConvertHelper.ToInt32(dtSettings.Rows[0]["SettingId"]);
                settingBase.SettingValue = dtSettings.Rows[0]["SettingValue"].ToString();
                settingBase.SettingText = dtSettings.Rows[0]["SettingText"].ToString();
                return settingBase;
            }else
                return null;
        }

        public static bool CheckLocalSettingsWithServer(ESettingsBase settingsBase)
        {
            bool filesToDelete = false;
            ESettingsBase settingsDeploymentDate = (ESettingsBase)settingsBase.Clone();
            ESettingsBase settingsDeploymentDateLocal = (ESettingsBase)settingsBase.Clone();
            LXmlSettings m_xmlSettingsManager = new LXmlSettings();
            settingsDeploymentDateLocal = m_xmlSettingsManager.GetSetting(settingsDeploymentDate);

            ESettingsBase settingsDeploymentDateServer = (ESettingsBase)settingsBase.Clone();
            settingsDeploymentDateServer = LSettings.GetSetting(settingsDeploymentDateServer);


            if (settingsDeploymentDateLocal != null && settingsDeploymentDateServer != null)
            {

                DateTime dateDeploymentLocal = DateTime.Parse(settingsDeploymentDateLocal.SettingText);
                DateTime dateDeploymentServer = DateTime.Parse(settingsDeploymentDateServer.SettingText);

                if (dateDeploymentLocal < dateDeploymentServer)
                {
                    filesToDelete = true;
                    m_xmlSettingsManager.UpdateNode(settingsDeploymentDateServer);
                }

            }
            else
            {
                if (settingsDeploymentDateServer == null)
                {
                    settingsDeploymentDateServer = (ESettingsBase)settingsBase.Clone();
                    settingsDeploymentDateServer.SettingId = 1;
                    settingsDeploymentDateServer.SettingValue = "1";
                    settingsDeploymentDateServer.SettingText = DateTime.Now.Date.ToShortDateString();
                }
                m_xmlSettingsManager.InsertNode(settingsDeploymentDateServer);
                filesToDelete = true;
            }
            return filesToDelete;

        }
    }
}
